package com.teddy.view.tree;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.teddy.model.skos.SKOS;

public class ThesaurusTreeITem extends SemanticTreeItem{
	private String label;
	private Resource res;

	public ThesaurusTreeITem(Tree modelTreeItem, int style) {
		super(modelTreeItem, style);
	}
	
	public ThesaurusTreeITem(Tree modelTreeItem, int style, Resource res) {
		super(modelTreeItem, style);
		this.res = (Resource) res;
		setText();
		setItemCount(1);
	}
	
	public ThesaurusTreeITem(TreeItem modelTreeItem, int style, Resource res, String lang) {
		super(modelTreeItem, style);
		this.res = (Resource) res;
		setSelectedLanguage(lang);
		setText();
		label = getText();
		setItemCount(1);
	}
	
	public void setText(){
		StmtIterator it =  res.listProperties(SKOS.prefLabel);
		while(it.hasNext()){
			Statement s = it.next();
			if(getSelectedLanguage()!= null){
				this.setText(s.getString());
				
				if (s.getLanguage().equals(getSelectedLanguage()))
					break;
			}
			else{
				this.setText(s.getString());
				break;
			}
		}
	}

	@Override
	public void expand() {
		// TODO Auto-generated method stub
		this.removeAll();
		StmtIterator it = res.listProperties(SKOS.narrower);
		while(it.hasNext()){
			Statement s = it.next();
			ThesaurusTreeITem item = new ThesaurusTreeITem(this, SWT.NONE,s.getResource(), getSelectedLanguage());
		}
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void remove() {
		res.getModel().remove(res.listProperties());
		//((ModelTreeItem)(TreeItem)(this.getParent())).expand();
		getParent().redraw();
	}

	@Override
	public void rename() {
		// TODO Auto-generated method stub
		
	}

	public String getPrefLAbel(){
		return getLabel();
	}
	
	public void setPrefLAbel(String label){
		setLabel(label);
		
	}
	public void setLabel(String label) {
		this.label = label;
	}

	public String getLabel() {
		return label;
	}
	
	public String getLiteralProperty(Property p){
		StmtIterator iter = res.listProperties(p);
		while(iter.hasNext()){
			Statement s = iter.next();
			if (s.getLanguage().equals(getSelectedLanguage())){
				return s.getString();
			}
		}
		return "";
	}
	
	public void setListeralProperty(Property p, String o){
		if (res.hasProperty(p))
			res.removeAll(p);
		res.addLiteral(p, o);
	}
	
	public List<Resource> getMultiplePropertyValue(Property p){
		List<Resource> list = new ArrayList<Resource>();
		StmtIterator it = res.listProperties(p);
		
		while(it.hasNext()){
			list.add(it.next().getResource());
		}
		return list;
	}
	
	public void addLiteralProperty(Property p, String o){
		res.addProperty(p, o, selectedLanguage);
	}
		
	public void removeFromMultipleResources(Property p, String o){
		StmtIterator it = res.listProperties(p);
		
		while (it.hasNext()){
			Statement s = it.next();
			if (s.getResource().getURI() == o){
				res.getModel().remove(s);
				break;
			}
		}
	}
	
	public void addToMultipleResources(Property p, String o){
		//res.addProperty(p, o);
		res.addProperty(p, res.getModel().createResource(o));
	}
	
	//public void addResource(Property p, Sring uri){
		//res.addp
	//}
	public List<Resource> getAllResources(){
		List<Resource> list = new ArrayList<Resource>();
		ResIterator it = res.getModel().listSubjects();
		while(it.hasNext()){
			Resource r = it.next();
			list.add(r);
		}
		return list;
	}
}
